import React from "react";
import { Link, Routes, Route, useNavigate } from "react-router-dom";

const Home = () => {
  const navigate = useNavigate();

  const fn = () => {
    // 页面的跳转
    // 编程式导航(用js的方式来做跳转)
    // 可以在跳转的时候去做一系列的逻辑判断等
    // navigate("/about");
    // navigate({
    //   pathname: "/about",
    // });

    // 如果后面跟的是整数，表示的是前进或者后退
    navigate(-1);
  };

  return (
    <div>
      这是首页 - <button onClick={fn}>跳转到关于页</button>
    </div>
  );
};
const About = () => {
  return <div>这是关于页</div>;
};

const App = () => {
  return (
    <>
      <h2>编程式导航</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
      </ul>
      <ul>
        <li>
          <Link to="/about">关于页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/home" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
      </Routes>
    </>
  );
};

export default App;
